﻿<!doctype html>
<html>
<head>
    <title>SpreadJS - Filter Action</title>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />

    <link href="../../../external/spreadjs/css/gcspread.sheets.excel2013white.9.40.20161.0.css" rel="stylesheet" type="text/css" />

    <script src="../../../external/external/jquery-1.8.2.min.js" type="text/javascript"></script>

    <script type="text/javascript" src="../../../external/spreadjs/gcspread.sheets.all.9.40.20161.0.min.js"></script>

    <script id="scriptInit" type="text/javascript">
        /*code_begin*/
        var spreadNS = GcSpread.Sheets;

        $(document).ready(function () {
            var spread = new GcSpread.Sheets.Spread(document.getElementById("ss"));
            initSpread(spread);
        });

        function initSpread(spread) {
            var sheet = spread.getSheet(0);
            sheet.isPaintSuspended(true);

            sheet.setColumnWidth(0, 100);
            sheet.setColumnWidth(1, 200);
            sheet.setValue(1, 0, "Name");
            sheet.setValue(1, 1, "Score");
            var score = [
                   { name: "Simon", score: 59 },
                   { name: "Jack", score: 70 },
                   { name: "Lily", score: 86 },
                   { name: "Bob", score: 54 },
                   { name: "Lucy", score: 84 },
                   { name: "John", score: 99 }
            ];
            for (var i = 0, len = score.length; i < len; i++) {
                var student = score[i];
                sheet.setValue(i + 2, 0, student.name);
                sheet.setValue(i + 2, 1, student.score);
            }

            function SelectionFilter(range) {
                spreadNS.RowFilterBase.call(this, range);
            }
            SelectionFilter.prototype = new spreadNS.RowFilterBase();
            SelectionFilter.prototype.onFilter = function (args) {
                var sheet = args.sheet, range = args.range,
                    filterRows = args.filteredRows,
                    filteredOutRows = args.filteredOutRows,
                    action = args.action,
                    doFilter = action === spreadNS.FilterActionType.Filter;

                if (doFilter) {
                    if (range.col < 0) {
                        range.col = 0;
                        range.colCount = sheet.getColumnCount();
                    }
                    var activeCellSet;
                    for (var i = 0, len = filterRows.length; i < len; i++) {
                        var r = filterRows[i];
                        for (var c = range.col, len1 = range.col + range.colCount; c < len1; c++) {
                            if (!activeCellSet) {
                                activeCellSet = true;
                                sheet.setSelection(r, c, 1, 1);
                            } else {
                                sheet.addSelection(r, c, 1, 1);
                            }
                        }
                    }
                    sheet.setValue(9, 1, "Select the filtered items");
                } else {
                    sheet.setSelection(0, 0, 1, 1);
                    sheet.setValue(9, 1, "No filtered items");
                }

                spread.focus();
            };

            var actionFilter = new SelectionFilter(new spreadNS.Range(2, 1, 6, 1));
            sheet.rowFilter(actionFilter);
            actionFilter.filterButtonVisible(false);

            sheet.isPaintSuspended(false);
            
            $("#btnFilter").click(function () {
                var sheet = spread.getActiveSheet(),
                    filter = sheet.rowFilter(),
                    value = parseInt($("#filterValue").val(), 10),
                    col = 1;

                if (filter && !isNaN(value)) {
                    filter.removeFilterItems(col);

                    var condition = new spreadNS.NumberCondition(spreadNS.GeneralCompareType.LessThan, value);
                    filter.addFilterItem(col, condition);

                    filter.filter();

                    sheet.invalidateLayout();
                    sheet.repaint();
                }
            });

            $("#btnClearFilter").click(function () {
                var sheet = spread.getActiveSheet(),
                    filter = sheet.rowFilter();

                if (filter) {
                    filter.removeFilterItems(1);
                }
            });
        }
        /*code_end*/
    </script>

</head>
<body>
<div class="sample-turtorial">
    <div id="ss" style="width:100%; height:340px;border: 1px solid gray;"></div>
    <div class="demo-options">
        <div class="option-row">
            <label for="filterValue">Score less than:</label>
            <input id="filterValue" placeholder="Input filter score value" type="text" value="60"/>

            <input type="button" id="btnFilter" value="Apply filter" title="Filter score column with filter action." />

            <input type="button" id="btnClearFilter" value="Clear filter" title="Clear score column' filter." />
        </div>
    </div>
</div>
</body>
</html>
